﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using store.Areas.Admin.Models;


namespace store.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        private readonly storeEntities _db = new storeEntities();

        //
        // GET: /Admin/Category/

        public ActionResult Index()
        {
            return View("Index", _db.Categories.ToList());
        }



        //
        // GET: /Admin/Category/Create

        public ActionResult Create()
        {
            ViewBag.Categorys = new SelectList(_db.Categories.ToList().AsEnumerable(), "CategoryID", "Name");
            CreateCategoryModel model = new CreateCategoryModel()
                {
                    Number = 0,
                    Active = true,
                };
            return View("Create", model);
        }

        //
        // POST: /Admin/Category/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateCategoryModel createCategoryModel)
        {
            ViewBag.Categorys = new SelectList(_db.Categories.ToList().AsEnumerable(), "CategoryID", "Name");
            if (ModelState.IsValid)
            {

                var category = new Category
                {
                    Name = createCategoryModel.Name,
                    ParentID = createCategoryModel.ParentID,
                    Number = createCategoryModel.Number,
                    Active = createCategoryModel.Active,
                    ModifiedDate = DateTime.Now.ToLocalTime()
                };
                _db.Categories.Add(category);
                _db.SaveChanges();
                if (createCategoryModel.OptionSubmit == "Lưu và Thêm mới")
                {
                    return RedirectToAction("Create");
                }
                return RedirectToAction("Index");
            }
            return View("Create", createCategoryModel);
        }

        //
        // GET: /Admin/Category/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Category category = _db.Categories.Find(id);
            ViewBag.Categorys = new SelectList(_db.Categories.ToList().AsEnumerable(), "CategoryID", "Name");
            if (category == null)
            {
                return HttpNotFound();
            }
            var editCategoryModel = new EditCategoryModel
                {
                    Active = category.Active != null && (bool)category.Active,
                    CategoryID = category.CategoryID,
                    Description = category.Description,
                    ModifiedDate = category.ModifiedDate,
                    Name = category.Name,
                    Number = category.Number,
                    ParentID = category.ParentID,
                    
                };
            return View(editCategoryModel);
        }

        //
        // POST: /Admin/Category/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(EditCategoryModel model)
        {
            if (ModelState.IsValid)
            {

                Category categoryUpdate = _db.Categories.Find(model.CategoryID);
                _db.Entry(categoryUpdate).CurrentValues.SetValues(model);

                _db.SaveChanges();
                if (model.OptionSubmit == "Lưu và Thoát")
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("Edit", new { id = model.CategoryID });
            }
            return View(model);
        }

        //
        // GET: /Admin/Category/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Category category = _db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            else
            {
                _db.Categories.Remove(category);
                _db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        //
        // POST: /Admin/Category/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = _db.Categories.Find(id);
            _db.Categories.Remove(category);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}